package com.atguigu.gmall.payment.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/10/15 14:07 星期六
 * description: 支付通知相关的消息队列配置
 */
@Configuration
public class PayRabbitConfig {

    /**
     * 创建支付的交换机
     * @return
     */
    @Bean("payExchange")
    public Exchange payExchange(){
        return ExchangeBuilder.directExchange("pay_exchange").build();
    }

    /**
     * 创建支付结果通知的队列: 普通订单
     * @return
     */
    @Bean("payOrderQueue")
    public Queue payOrderQueue(){
        return QueueBuilder.durable("pay_order_queue").build();
    }

    /**
     * 创建支付结果通知的队列: 秒杀订单
     * @return
     */
    @Bean
    public Queue paySeckillOrderQueue(){
        return QueueBuilder.durable("pay_seckill_order_queue").build();
    }

    /**
     * 秒杀指定队列和交换机绑定
     * @param paySeckillOrderQueue
     * @param payExchange
     * @return
     */
    @Bean
    public Binding seckillOrderBinding(@Qualifier("paySeckillOrderQueue") Queue paySeckillOrderQueue,
                                       @Qualifier("payExchange") Exchange payExchange){
        return BindingBuilder.bind(paySeckillOrderQueue).to(payExchange).with("seckill.order.pay").noargs();
    }


    /**
     * 普通订单的支付结果队列和交换机绑定
     * @param payExchange
     * @param payOrderQueue
     * @return
     */
    @Bean
    public Binding orderBinding(@Qualifier("payExchange") Exchange payExchange,
                                @Qualifier("payOrderQueue") Queue payOrderQueue){
        return BindingBuilder.bind(payOrderQueue).to(payExchange).with("order.pay").noargs();
    }
}
